// Copyright 2022 a.o.smith ALL Rights Reserved
package com.liuxinlong.modules.system.controller;

import com.liuxinlong.annotation.LogAnnotation;
import com.liuxinlong.common.AosException;
import com.liuxinlong.common.SnowFlake;
import com.liuxinlong.constant.LogBusinessConstants;
import com.liuxinlong.constant.LogOperateConstants;
import com.liuxinlong.enums.ReturnCodes;
import com.liuxinlong.modules.common.controller.request.BaseDeleteReq;
import com.liuxinlong.modules.common.controller.request.QueryBaseReq;
import com.liuxinlong.modules.common.controller.response.BaseResp;
import com.liuxinlong.modules.system.controller.request.QueryBackReq;
import com.liuxinlong.modules.system.service.BackupService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 数据备份控制层
 *
 * @author liuxinlong@hotwater.com.cn
 * @version 1.0.0
 * @since 2022-07-19
 */
@RestController
@RequestMapping("/backup")
public class BackupController {

    @Autowired
    private BackupService backupService;

    /**
     * 分页查询数据备份记录
     *
     * @param req 请求信息
     * @return 数据备份记录信息列表
     */
    @PostMapping("page")
    @LogAnnotation(value = "分页查询", business = LogBusinessConstants.BACKUP_MANAGE, operateType = LogOperateConstants.OPERATE_QUERY)
    public BaseResp pageBackupInfo(@RequestBody QueryBackReq req) {
        Map<String, Object> queryMap = new HashMap<>();
        queryMap.put("startNum", req.getStartNum());
        queryMap.put("pageSize", req.getPageSize());
        queryMap.put("name", req.getName());
        queryMap.put("businessType", req.getBusinessType());
        List<Map<String, Object>> dataList = backupService.pageBackupInfo(queryMap);
        int total = backupService.countBackupInfo(queryMap);
        BaseResp resp = new BaseResp();
        resp.setRetMessage("分页查询数据备份记录成功");
        resp.setDataList(dataList);
        resp.setTotalNum(total);
        return resp;
    }

    /**
     * 手动数据备份
     *
     * @return 成功标识
     */
    @PostMapping("start")
    @LogAnnotation(value = "新增数据备份记录信息", business = LogBusinessConstants.BACKUP_MANAGE, operateType = LogOperateConstants.OPERATE_ADD)
    public BaseResp startBackup(@RequestBody QueryBaseReq req) {
        if (StringUtils.isEmpty(req.getId())) {
            throw new AosException(ReturnCodes.HTTP_PARAM_ERROR);
        }
        String backupId = SnowFlake.nextIdStr();
        String fileId = SnowFlake.nextIdStr();
        int type = Integer.parseInt(req.getId());
        String backupPath = type == 1 ? ("/dataBackup" + fileId + ".sql") : ("/fileBackup" + fileId + ".zip");
        backupService.createInfo(backupId, fileId, backupPath, type);
        backupService.startBackup(backupId, fileId, backupPath, type);
        BaseResp resp = new BaseResp();
        resp.setRetMessage("数据备份成功");
        return resp;
    }

    /**
     * 删除数据备份
     *
     * @return 成功标识
     */
    @PostMapping("delete")
    @LogAnnotation(value = "删除数据备份记录信息", business = LogBusinessConstants.BACKUP_MANAGE, operateType = LogOperateConstants.OPERATE_DELETE)
    public BaseResp deleteBackup(@RequestBody BaseDeleteReq req) {
        backupService.deleteBackup(req.getId());
        BaseResp resp = new BaseResp();
        resp.setRetMessage("删除数据备份记录成功");
        return resp;
    }

    /**
     * 导出数据备份记录信息
     *
     * @return 导出数据
     */
    @PostMapping("export")
    @LogAnnotation(value = "导出数据备份记录信息", business = LogBusinessConstants.BACKUP_MANAGE, operateType = LogOperateConstants.OPERATE_EXPORT)
    public void exportBackup(HttpServletResponse response) {
        backupService.exportBackup(response);
    }
}
